import 'package:flutter/material.dart';
import 'package:flutter_jd/widget/jdButton.dart';
import '../../services/UserServices.dart';
import '../../services/ScreenAdatper.dart';
class UserPage extends StatefulWidget {
  UserPage({Key key}) : super(key: key);

  _UserPageState createState() => _UserPageState();
}




class _UserPageState extends State<UserPage> {
  bool _isLogin = false;
  Map _userInfo;
  @override
  void initState() { 
    super.initState();
    this.getUserInfo();
  }
  getUserInfo() async{
     bool isLogin = await UserServices.getUserLoginState();
     List userInfo = await UserServices.getUserInfo();
     print("isLogin == ${isLogin}");
     if(isLogin){
        setState((){
          this._isLogin = true;
          this._userInfo = userInfo[0];
        });
     }
  }

  outLogin(){
    UserServices.loginOut();
    setState(() {
      this._isLogin = false;
    });
  }
  @override
  Widget build(BuildContext context) {
    ScreenAdatper.init(context);
    return Scaffold(
      body: ListView(
        children: <Widget>[
          Container(
            height: ScreenAdatper.height(220),
            width: double.infinity,
            decoration: BoxDecoration(
              image: DecorationImage(image: AssetImage('images/user_bg.jpg'),fit: BoxFit.cover)
            ),
            child: Row(
              children: <Widget>[
                Container(
                  margin: EdgeInsets.fromLTRB(15, 0, 15, 0),
                  child: ClipOval(
                    child: Image.asset('images/user.png',fit:BoxFit.cover,width: ScreenAdatper.width(100),height: ScreenAdatper.height(100),),
                  ),
                ),
                Expanded(
                  flex: 1,
                  child:InkWell(
                    onTap:(){
                      Navigator.pushNamed(context, '/login');
                    },
                    child: this._isLogin ? Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text('用户名：${_userInfo['username']}',style: TextStyle(color: Colors.white,fontSize: ScreenAdatper.width(32)),) ,
                        Text('普通会员',style: TextStyle(color: Colors.white,fontSize: ScreenAdatper.width(24)),) ,
                      ],
                    ):  Text('注册/登陆',style: TextStyle(color: Colors.white),) ,
                  ),
                )
              ],
            ),
          ),
          ListTile(
            leading: Icon(Icons.home,color: Colors.red,),
            title: Text("全部订单"),
          ),
          Divider(),
           ListTile(
            leading: Icon(Icons.payment,color: Colors.green,),
            title: Text("待付款"),
          ),
          Divider(),
           ListTile(
            leading: Icon(Icons.local_car_wash,color: Colors.orange,),
            title: Text("待收货"),
          ),
          Container(
             height: 20,
             width: double.infinity,
             color: Colors.black12, 
          ),
           ListTile(
            leading: Icon(Icons.favorite,color: Colors.lightGreen,),
            title: Text("我的收藏"),
          ),
          Divider(),
           ListTile(
            leading: Icon(Icons.people,color: Colors.black54,),
            title: Text("在线客服"),
          ),
          _isLogin ? Container(
            width:100.0,
            margin: EdgeInsets.only(top: 50),
            child: JdButtoon(
                color: Colors.red,
                text: "退出登录",
                onTap: this.outLogin,
              ),
          ):Text("")
        ],
      ),
    );
  }
}